package stark.app.function.view;

import android.content.Context;
import android.content.Intent;

import com.hjq.bar.TitleBar;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import stark.common.basic.base.BaseTitleBarFragmentActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.IntentUtil;

/**
 * Author：daizhongbin on 2021/7/29
 * Email：1027154338@qq.com
 * Desc:
 */
public class ShowViewFragmentActivity extends BaseTitleBarFragmentActivity {

    public static void start(Context context, Class<? extends Fragment> fragmentClass) {
        Intent intent = IntentUtil.getIntent(context, ShowViewFragmentActivity.class);
        intent.putExtra(Extra.CONTENT, fragmentClass);
        context.startActivity(intent);
    }

    @Override
    protected void onConfigTitleBar(TitleBar titleBar) {
        super.onConfigTitleBar(titleBar);
        titleBar.setTitle("自定义控件使用DEMO");
    }

    @NonNull
    @Override
    protected Fragment getFragment() {
        Intent intent = getIntent();
        Class<? extends Fragment> fragmentClass = (Class<? extends Fragment>) intent.getSerializableExtra(Extra.CONTENT);
        return createFragment(fragmentClass);
    }

    private Fragment createFragment(Class<? extends Fragment> fragmentClass) {
        try {
            Fragment fragment = fragmentClass.newInstance();
            return fragment;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
